package text.adventure.input;
import java.util.ArrayList;
import java.util.List;

import text.adventure.blackboard.Interpreter;
import text.adventure.data.TextCommand;




public class Parser {
	//Just test
	public TextCommand testReturn;
	
	private Interpreter interpreter;
	private SynFilter synFilter;
	
	private List<String> nouns;
	private List<String> verbs;
	private List<String> other;
	
	public Parser(List<String> nouns, List<String> verbs, List<String> other)
	{
		this.interpreter = new Interpreter();
		this.nouns = nouns;
		this.verbs = verbs;
		this.other = other;
	}
	
	public Parser(List<String> nouns, List<String> verbs, List<String> other, SynFilter syn)
	{
		this.interpreter = new Interpreter();
		this.synFilter = syn;
		this.nouns = nouns;
		this.verbs = verbs;
		this.other = other;
	}
	
	public void setSynFilter(SynFilter syn)
	{
		this.synFilter = syn;
	}
	
	public boolean processInput(String input)
	{
		String regularChars = removeSpecialCharacters(input);
		String[] words = regularChars.split("\\s+");
		
		//verb
		if (!verbs.contains(words[0])) return false;	
		String verb = words[0];
		
		//objects
		List<String> objects = new ArrayList<String>();
		for (String word : words) {
			if (word == verb) continue;
			if (nouns.contains(word)) {
				objects.add(word);
			} else if (!other.contains(word)) {
				return false;
			}
		}
		
		TextCommand command = new TextCommand(true, verb, objects);
		TextCommand parsedCommand = synFilter.filter(command);
		testReturn = parsedCommand;
		interpreter.doStuffWithCommand(parsedCommand);
		return true;
	}
	
	private String removeSpecialCharacters(String line)
	{
		//TODO
		return line;
	}
}
